package com.wangrui.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.wangrui.client.GreetingService;
import com.wangrui.client.data.BeanSipfLogDTO;
import com.wangrui.client.data.BeanSipfa01DTO;
import com.wangrui.client.data.BeanSipfa04DTO;
import com.wangrui.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService {

	String url = "jdbc:oracle:thin:@168.100.101.30:1521:tbjj";//oracle连接
	//String url = "jdbc:mysql://localhost:3306/test";
	
	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}

		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");

		// Escape data from the client to avoid cross-site script vulnerabilities.
		input = escapeHtml(input);
		userAgent = escapeHtml(userAgent);

		return "Hello, " + input + "!<br><br>I am running " + serverInfo
				+ ".<br><br>It looks like you are using:<br>" + userAgent;
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	@Override
	public boolean login(String username, String password) throws Exception {

		// public boolean login(Map formData)throws Exception{

		//System.out.println(username);
		Class.forName("oracle.jdbc.driver.OracleDriver"); //oracle连接
		//Class.forName("com.mysql.jdbc.Driver");   //mysql连接
		
		Connection conn = DriverManager.getConnection(url, "hsman", "hstbjj");

		String sql = "select *  from userlist where CODE='" + username
				+ "'and PSW = '" + password + "'";

		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);

		if (rs.next())
			return true;
		else
			return false;

	}
	
public List getSipfa01(){
		
		try {
			
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url, "hsman", "hstbjj");
			
			String sql = "select * from sipfa01 order by OC_DATE desc";
			
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			List sipfa01Data = new ArrayList();

			while(rs.next()){
				
				BeanSipfa01DTO sipfa04Bean  = new BeanSipfa01DTO();
				
				sipfa04Bean.setOC_DATE(rs.getString("OC_DATE"));
				sipfa04Bean.setZQGSDM(rs.getString("ZQGSDM"));
				sipfa04Bean.setZQGSMC(rs.getString("ZQGSMC"));
				sipfa04Bean.setJSCYRDM(rs.getString("JSCYRDM"));
				sipfa04Bean.setJJXSRDM(rs.getString("JJXSRDM"));
				sipfa04Bean.setJSCYRJSZH(rs.getString("JSCYRJSZH"));
				sipfa04Bean.setJSCYRQSBH(rs.getString("JSCYRQSBH"));
				sipfa04Bean.setSJRQ(rs.getString("SJRQ"));
				sipfa01Data.add(sipfa04Bean);

			}
			return sipfa01Data;
	
		}catch(Exception e){
			
			e.printStackTrace();
		}		
		return null;
	}
	
	public List getSipfLog() {
		
		try{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		//Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection(url, "hsman", "hstbjj");
		
		String sql = "select OC_DATE,PERSON,STATUS from sipflog_test";
		
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		
		List sipflogData = new ArrayList();
		
		while(rs.next()){
			
			BeanSipfLogDTO sipflogBean = new BeanSipfLogDTO(); 
			sipflogBean.setOC_DATE(rs.getString("OC_DATE"));
			sipflogBean.setSTATUS(rs.getString("STATUS"));
			sipflogBean.setPERSON(rs.getString("PERSON"));
			
			sipflogData.add(sipflogBean);
		}
		
		return sipflogData;
	
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
		
	}
	
	public boolean addSipfLog(String OC_DATE, String PERSON, String STATUS,
			String CLNHISSTART, String CLNHISEND, String CLNHISSUM,
			String PREHANDSTART, String PREHANDEND, String PREHANDSUM,
			String INCRSTART, String INCREND, String INCRSUM,
			String INCREXPORTSTART, String INCREXPORTEND, String INCREXPORTSUM,
			String INCRCHKSTART, String INCRCHKEND, String INCRCHKSUM,
			String FULLSTART, String FULLEND, String FULLSUM,
			String FULLCHKSTART, String FULLCHKEND, String FULLCHKSUM,
			String FULLEXPORTSTART, String FULLEXPORTEND, String FULLEXPORTSUM,
			String FILESTART, String FILEEND, String FILESUM,
			String FILECHKSTART, String FILECHKEND, String FILECHKSUM,
			String FILEEXPORTSTART, String FILEEXPORTEND, String FILEEXPORTSUM,
			String FILEUPLOADSTART, String FILEUPLOADEND, String FILEUPLOADSUM,
			String APPSTATUS, String COMPUTERSTATUS, String REMARK)
			throws Exception {
		
		Class.forName("oracle.jdbc.driver.OracleDriver");
		//Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection(url, "hsman", "hstbjj");
		String sql = "insert into sipflog_test(OC_DATE,PERSON,STATUS,CLNHISSTART,CLNHISEND,CLNHISSUM,PREHANDSTART,PREHANDEND,PREHANDSUM,INCRSTART, INCREND,INCRSUM,INCREXPORTSTART,INCREXPORTEND,INCREXPORTSUM," +
				"INCRCHKSTART,INCRCHKEND,INCRCHKSUM,FULLSTART,FULLEND,FULLSUM,FULLCHKSTART,FULLCHKEND,FULLCHKSUM,FULLEXPORTSTART,FULLEXPORTEND,FULLEXPORTSUM,FILESTART,FILEEND,FILESUM," +
				"FILECHKSTART,FILECHKEND,FILECHKSUM,FILEEXPORTSTART,FILEEXPORTEND,FILEEXPORTSUM,FILEUPLOADSTART,FILEUPLOADEND,FILEUPLOADSUM,APPSTATUS,COMPUTERSTATUS,REMARK) values('" +OC_DATE + "','" +PERSON + "','" +STATUS + "','" +CLNHISSTART + "','" +CLNHISEND + "','" +CLNHISSUM + "','" +PREHANDSTART + "','" +PREHANDEND + "','" +PREHANDSUM + "','" +INCRSTART + "','" +INCREND + "','" +INCRSUM + "','" +INCREXPORTSTART + "','" +INCREXPORTEND + "','" +INCREXPORTSUM + "','" +INCRCHKSTART + "','" +INCRCHKEND + "','" +INCRCHKSUM + "','" +FULLSTART + "','" +FULLEND + "','" +FULLSUM + "','" +FULLCHKSTART + "','" +FULLCHKEND + "','" +FULLCHKSUM + "','" +FULLEXPORTSTART + "','" +FULLEXPORTEND + "','" +FULLEXPORTSUM + "','" +FILESTART + "','" +FILEEND + "','" +FILESUM + "','" +FILECHKSTART + "','" +FILECHKEND + "','" +FILECHKSUM + "','" +FILEEXPORTSTART + "','" +FILEEXPORTEND + "','" +FILEEXPORTSUM + "','" +FILEUPLOADSTART + "','" +FILEUPLOADEND + "','" +FILEUPLOADSUM + "','" +APPSTATUS + "','" +COMPUTERSTATUS + "','" +REMARK + "')";
		Statement stmt = conn.createStatement();
		
	    stmt.executeUpdate(sql);
	    
	    System.out.println(sql+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		
		return true;
	}
	
	public List getSipfLogForm() throws Exception {
		
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url, "hsman", "hstbjj");
			
			String sql = "select * from sipflog_test";
			
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			List sipflogData = new ArrayList();
			
			while(rs.next()){
				
				BeanSipfLogDTO sipflogBean = new BeanSipfLogDTO(); 
				sipflogBean.setOC_DATE(rs.getString("OC_DATE"));
				sipflogBean.setSTATUS(rs.getString("STATUS"));
				sipflogBean.setPERSON(rs.getString("PERSON"));
				
				sipflogData.add(sipflogBean);
			}
			
			return sipflogData;

			}catch (Exception e){
				e.printStackTrace();
			}
			return null;
	}
	
	public List getSipfa04() throws Exception {

		try {
			
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url, "hsman", "hstbjj");
			
			String sql = "select * from sipfa04 order by OC_DATE desc";
			
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			System.out.println(sql+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			List sipfa04Data = new ArrayList();

			while(rs.next()){
				
				BeanSipfa04DTO sipfa04Bean  = new BeanSipfa04DTO();
				
				sipfa04Bean.setOC_DATE(rs.getString("OC_DATE"));
				sipfa04Bean.setZQGSDM(rs.getString("ZQGSDM"));
				sipfa04Bean.setYYBNBDM(rs.getString("YYBNBDM"));
				sipfa04Bean.setKHDM(rs.getString("KHDM"));
				sipfa04Bean.setKHKHRQ(rs.getString("KHKHRQ"));
				sipfa04Bean.setKHZXRQ(rs.getString("KHZXRQ"));
				sipfa04Bean.setKHMC(rs.getString("KHMC"));
				sipfa04Bean.setKHLX(rs.getString("KHLX"));
				sipfa04Bean.setKHZT(rs.getString("KHZT"));
				sipfa04Bean.setZJLX1(rs.getString("ZJLX1"));
				sipfa04Bean.setZJHM1(rs.getString("ZJHM1"));
				sipfa04Bean.setZJYXJZR1(rs.getString("ZJYXJZR1"));
				sipfa04Bean.setZJLX2(rs.getString("ZJLX2"));
				sipfa04Bean.setZJHM2(rs.getString("ZJHM2"));
				sipfa04Bean.setZJYXJZR2(rs.getString("ZJYXJZR2"));
				sipfa04Bean.setKHGJ(rs.getString("KHGJ"));
				sipfa04Bean.setKHXL(rs.getString("KHXL"));
				sipfa04Bean.setKHZY(rs.getString("KHZY"));
				sipfa04Bean.setFRDB(rs.getString("FRDB"));
				sipfa04Bean.setFRDBZJHM(rs.getString("FRDBZJHM"));
				sipfa04Bean.setJBR(rs.getString("JBR"));
				sipfa04Bean.setJBRZJHM(rs.getString("JBRZJHM"));
				sipfa04Bean.setZCDZ(rs.getString("ZCDZ"));
				sipfa04Bean.setLXDZ(rs.getString("LXDZ"));
				sipfa04Bean.setYZBM(rs.getString("YZBM"));
				sipfa04Bean.setLXDH(rs.getString("LXDH"));
				sipfa04Bean.setSJHM(rs.getString("SJHM"));
				sipfa04Bean.setDZYX(rs.getString("DZYX"));
				sipfa04Bean.setCGMS(rs.getString("CGMS"));
				sipfa04Bean.setSJRQ(rs.getString("SJRQ"));
				sipfa04Data.add(sipfa04Bean);

			}
			return sipfa04Data;
	
		}catch(Exception e){
			
			e.printStackTrace();
		}		
		return null;
	}

	public static void  main(String args[]) throws  Exception{
	
		
		//Class.forName("com.mysql.jdbc.Driver"); 
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@168.100.101.30:1521:tbjj", "hsman", "hstbjj");
		
		String sql = "select * from sipfa04 order by OC_DATE desc";
		
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		
		List sipfa04Data = new ArrayList();

		while(rs.next()){
			
			BeanSipfa04DTO sipfa04Bean  = new BeanSipfa04DTO();
			
			sipfa04Bean.setOC_DATE(rs.getString("OC_DATE"));
			sipfa04Bean.setZQGSDM(rs.getString("ZQGSDM"));
			sipfa04Bean.setYYBNBDM(rs.getString("YYBNBDM"));
			sipfa04Bean.setKHDM(rs.getString("KHDM"));
			sipfa04Bean.setKHKHRQ(rs.getString("KHKHRQ"));
			sipfa04Bean.setKHZXRQ(rs.getString("KHZXRQ"));
			sipfa04Bean.setKHMC(rs.getString("KHMC"));
			sipfa04Bean.setKHLX(rs.getString("KHLX"));
			sipfa04Bean.setKHZT(rs.getString("KHZT"));
			sipfa04Bean.setZJLX1(rs.getString("ZJLX1"));
			sipfa04Bean.setZJHM1(rs.getString("ZJHM1"));
			sipfa04Bean.setZJYXJZR1(rs.getString("ZJYXJZR1"));
			sipfa04Bean.setZJLX2(rs.getString("ZJLX2"));
			sipfa04Bean.setZJHM2(rs.getString("ZJHM2"));
			sipfa04Bean.setZJYXJZR2(rs.getString("ZJYXJZR2"));
			sipfa04Bean.setKHGJ(rs.getString("KHGJ"));
			sipfa04Bean.setKHXL(rs.getString("KHXL"));
			sipfa04Bean.setKHZY(rs.getString("KHZY"));
			sipfa04Bean.setFRDB(rs.getString("FRDB"));
			sipfa04Bean.setFRDBZJHM(rs.getString("FRDBZJHM"));
			sipfa04Bean.setJBR(rs.getString("JBR"));
			sipfa04Bean.setJBRZJHM(rs.getString("JBRZJHM"));
			sipfa04Bean.setZCDZ(rs.getString("ZCDZ"));
			sipfa04Bean.setLXDZ(rs.getString("LXDZ"));
			sipfa04Bean.setYZBM(rs.getString("YZBM"));
			sipfa04Bean.setLXDH(rs.getString("LXDH"));
			sipfa04Bean.setSJHM(rs.getString("SJHM"));
			sipfa04Bean.setDZYX(rs.getString("DZYX"));
			sipfa04Bean.setCGMS(rs.getString("CGMS"));
			sipfa04Bean.setSJRQ(rs.getString("SJRQ"));
			sipfa04Data.add(sipfa04Bean);
			
		//System.out.println(sipfa01Data.toArray().length);
		}
		BeanSipfa04DTO[] array = new BeanSipfa04DTO[sipfa04Data.size()];
		for(int i=0;i<sipfa04Data.size();i++){
			
			array[i] = (BeanSipfa04DTO) sipfa04Data.get(i);
			
			System.out.println("A04:"+array[i].OC_DATE);

		}
	}

	

}
